#include<iostream>
#include <iostream>
#include <fstream>

using namespace std;


struct BinaryTreeNode{
    int value;
    BinaryTreeNode * left;
    BinaryTreeNode * right;
};

BinaryTreeNode * createNode(int value){
    BinaryTreeNode * node = new BinaryTreeNode();
    node -> value = value;
    node -> left = nullptr;
    node ->  right = nullptr;
    return node;
}

void connectNode(BinaryTreeNode * root, BinaryTreeNode * left, BinaryTreeNode * right){
    root -> left = left;
    root -> right = right;
}


void printNode (BinaryTreeNode * head){
    BinaryTreeNode * root = head;
    if ( head != nullptr ){
        cout << root -> value << "  ";
        printNode(root->left);
        printNode(root->right);
    }
}

// 序列化
void Serialize(BinaryTreeNode * root, ostream& stream){
    BinaryTreeNode * pRoot = root;
    if ( root == nullptr ){
        stream << "$,";
        return;
    }
    stream << pRoot->value << ',';
    Serialize(pRoot->left,stream);
    Serialize(pRoot->right,stream);
}

int main(void){
    BinaryTreeNode * head = createNode(1);
    BinaryTreeNode * node2 = createNode(2);
    BinaryTreeNode * node3 = createNode(3);
    BinaryTreeNode * node4 = createNode(4);
    BinaryTreeNode * node5 = createNode(5);
    BinaryTreeNode * node6 = createNode(6);

    connectNode(head, node2, node3);
    connectNode(node2, node4, node5);
    connectNode(node3, nullptr, node5);
    connectNode(node4, node6, nullptr);

    char const * fileName = "/home/ubuntu/test.txt";
    ofstream fileOut;
    fileOut.open(fileName);
	Serialize(head, fileOut);
    fileOut.close();

    return 0;
}